package com.smart.common.security;

import java.util.Map;
import javax.annotation.Resource;
import com.smart.entity.SysUser;
import com.smart.service.CarService;
import com.smart.common.util.SysUserDetails;
import org.springframework.stereotype.Service;
import com.smart.common.exception.ApiException;
import com.smart.common.exception.SysExceptionEnum;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * @author lgb
 * @date 2020年6月15日 上午9:00:05
 * Copyright (C) 深圳工勘科技
 * @说明: 用户详情
 **/
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService{
	
    @Resource
	private CarService carService;
	@Resource
	private com.smart.service.UserService userService;
    @Override
    public UserDetails loadUserByUsername(String un) throws UsernameNotFoundException{
    	// 获取登录用户信息
		SysUser user = userService.getByUserName(un);
		if (user==null) {
			throw new ApiException(SysExceptionEnum.WRONG_USERNAME_OR_PASSWORD);
		}
		Map<String, Object> car = carService.getCarListByUserId(user.getId());
		if (car == null) {
			return new SysUserDetails(user, "");
		}
		return new SysUserDetails(user,car.get("car_no").toString());
    }
    
}